package com.banggood.ppsprojectarch.controller;

import com.banggood.pps.commons.controller.BaseController;
import com.banggood.pps.commons.service.BaseService;
import com.banggood.ppsprojectarch.bean.dto.UserDTO;
import com.banggood.ppsprojectarch.bean.entity.User;
import com.banggood.ppsprojectarch.bean.vo.UserVO;
import com.banggood.ppsprojectarch.service.UserService;
import com.banggood.silk.web.api.common.PagedResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Map;

@Api(value = "user", description = "管理")
@RestController
@RequestMapping("/user")
public class UserController extends BaseController<User> {
    @Autowired
    UserService userService;

    @Override
    protected BaseService<User> getService() {
        return userService;
    }

    @ApiOperation(value = "获取")
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public UserVO get(Long userId) throws Exception {
        return convert(handleGet(userId), UserVO.class);
    }

    @ApiOperation(value = "新增")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public int add(@Valid @RequestBody UserDTO userDto) throws Exception {
        return handleAdd(convert(userDto, User.class), (User record, Long primaryKey) -> {
            record.setUserId(primaryKey);
        });
    }

    @ApiOperation(value = "更新")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public int update(@Valid @RequestBody UserDTO UserDto) throws Exception {
        return handleUpdate(convert(UserDto, User.class));
    }

    @ApiOperation(value = "删除")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public int delete(Long userId) throws Exception {
        return handleDelete(userId);
    }

    @ApiOperation(value = "获取列表")
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public PagedResult<UserVO> search(@RequestBody Map<String, Object> map) {
        return convertPagedResult(handleSearch(map), UserVO.class);
    }
}